package com.arges.web.serviceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.arges.web.common.DateUtil;
import com.arges.web.dao.GenericDao;
import com.arges.web.dao.PatDao;
import com.arges.web.dao.PthlgyDao;
import com.arges.web.dao.TreatDao;
import com.arges.web.domain.bean.PatBean;
import com.arges.web.domain.pojo.Pat;
import com.arges.web.domain.pojo.Pthlgy;
import com.arges.web.domain.pojo.Treat;
import com.arges.web.page.PageInfo;
import com.arges.web.page.WebUtils;
import com.arges.web.service.PatManager;
import com.arges.web.service.TreatManager;

/**
 * 患者基本信息业务层
 *
 * @author zxj
 *
 */
@Service
public class PatManagerImpl extends GenericManageImpl<Pat, PatBean> implements PatManager
{
	private Logger LOGGER = LoggerFactory.getLogger(PatManagerImpl.class);

	@Autowired
	private PatDao patDao;

	@Autowired
	private PthlgyDao pthlgyDao;

	@Autowired
	private TreatDao treatDao;

	@Autowired
	private TreatManager treatManager;

	@Override
	public GenericDao<Pat, Long> getGenericDao()
	{
		// TODO Auto-generated method stub
		return patDao;
	}

	@Override
	public PatBean getPatByMrn(String mrn)
	{
		PatBean patBean = null;
		Pat pat = patDao.getPatByMrn(mrn);
		try
		{
			if (null != pat)
			{
				patBean = new PatBean();
				BeanUtils.copyProperties(pat, patBean);
			}
		}
		catch (Exception e)
		{
			LOGGER.error("find pat by mrn is error . mrn=" + mrn);
		}
		return patBean;
	}

	@Override
	public PageInfo<PatBean> getPagePat(PageInfo<Pat> pageInfo, String mrn)
	{
		PageInfo<PatBean> beanPageList = null;
		try
		{
			Criterion criterion = null;
			if (StringUtils.isNotBlank(mrn.trim()))
			{
				criterion = Restrictions.like("mrn", mrn, MatchMode.ANYWHERE);
			}
			PageInfo<Pat> pojoPageList = patDao.findAll(pageInfo, Pat.class, criterion);

			beanPageList = WebUtils.pojoToBeanPage(pojoPageList, PatBean.class);

			// List<Work> list = workDao.findAll();

			// for (DocBean docBean : beanPageList.getResult())
			// {
			// for (Work work : list)
			// {
			// if (docBean.getEno().equals(work.getEno()))
			// {
			// docBean.setD_name(work.getD_name());
			// }
			// }
			// }
		}
		catch (Exception e)
		{
			return null;
		}
		return beanPageList;
	}

	@Override
	public PatBean findPatById(long parseLong)
	{
		Pat pat = patDao.get(parseLong);
		PatBean patBean = null;
		if (null != pat)
		{
			patBean = new PatBean();
			BeanUtils.copyProperties(pat, patBean);
		}
		// Work work = workDao.getByEno(docBean.getEno());
		// if (work != null)
		// {
		//
		// }
		return patBean;
	}

	@Override
	public void deleteByIds(String ids)
	{
		String[] split = ids.split(",");
		Long[] deleteId = new Long[split.length];
		for (int i = 0; i < split.length; i++)
		{
			deleteId[i] = Long.valueOf(split[i]);
		}
		patDao.deletePatByIds(deleteId);
	}

	@Override
	public PatBean getLastPatBean()
	{
		PatBean patBean = new PatBean();
		Pat pat = patDao.getLastPat();
		if (null != pat)
		{
			patBean = new PatBean();
			BeanUtils.copyProperties(pat, patBean);
		}
		return patBean;
	}

	@SuppressWarnings("unused")
	@Override
	public List<PatBean> getPatBeanList(String startTime, String endTime)
	{
		List<PatBean> patBeanList = null;
		Criterion condiition = null;
		if (StringUtils.isNotBlank(startTime) && (StringUtils.isNotBlank(endTime)))
		{
			Date startDate = DateUtil.convertString2Date(startTime, DateUtil.DEFAULT_DATE_TIME);
			Date endDate = DateUtil.convertString2Date(endTime, DateUtil.DEFAULT_DATE_TIME);
			Criterion timeCriterion = null;
			timeCriterion = Restrictions.between("date", startDate, endDate);
			if (null == condiition)
			{
				condiition = timeCriterion;
			}
			else
			{
				condiition = Restrictions.and(condiition, timeCriterion);
			}
		}
		else if (StringUtils.isNotBlank(startTime))
		{
			Date startDate = DateUtil.convertString2Date(startTime, DateUtil.DEFAULT_DATE_TIME);
			Criterion timeCriterion = null;
			timeCriterion = Restrictions.ge("date", startDate);
			if (null == condiition)
			{
				condiition = timeCriterion;
			}
			else
			{
				condiition = Restrictions.and(condiition, timeCriterion);
			}
		}
		else if (StringUtils.isNotBlank(endTime))
		{
			Date endDate = DateUtil.convertString2Date(endTime, DateUtil.DEFAULT_DATE_TIME);
			Criterion timeCriterion = null;
			timeCriterion = Restrictions.le("date", endDate);
			if (null == condiition)
			{
				condiition = timeCriterion;
			}
			else
			{
				condiition = Restrictions.and(condiition, timeCriterion);
			}
		}
		List<Pat> list = patDao.findByDate(Pat.class, condiition);
		List<Pthlgy> pthlgyList = pthlgyDao.findAll();
		// List<Treat> treatList = treatDao.findAll();

		Map<String, Pthlgy> pthlgyMap = new HashMap<String, Pthlgy>();

		for (Pthlgy pthlgy : pthlgyList)
		{
			pthlgyMap.put(pthlgy.getMrn(), pthlgy);
		}
		if (null != list)
		{
			patBeanList = new ArrayList<PatBean>();
			for (Pat pat : list)
			{
				PatBean patBean = new PatBean();
				BeanUtils.copyProperties(pat, patBean);
				BeanUtils.copyProperties(pthlgyMap.get(pat.getMrn()), patBean);
				List<Treat> treatList = treatManager.findTreatBymrn(pat.getMrn());
				String treatString = "";
				for (Treat treat : treatList)
				{
					treatString = treatString + treat.getTherary() + ",";
				}
				if (treatString.length() > 0)
				{
					treatString = treatString.substring(0, treatString.length() - 1);
				}
				patBean.setZlfsCount(treatString);
				patBeanList.add(patBean);
			}
		}

		return patBeanList;
	}
}
